package com.xywh.wanandroid.util

import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import com.xywh.wanandroid.data.model.bean.LoginBean
import com.xywh.wanandroid.data.model.bean.UserInfoBean

object CacheUtil {
    /**
     * 获取保存的账户信息
     */
    fun getUser(): LoginBean? {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("user")
        return if (TextUtils.isEmpty(userStr)) {
            null
        } else {
            Gson().fromJson(userStr, LoginBean::class.java)
        }
    }

    fun getUserInfoBean(): UserInfoBean? {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("userInfoBean")
        return if (TextUtils.isEmpty(userStr)) {
            null
        } else {
            Gson().fromJson(userStr, UserInfoBean::class.java)
        }
    }

    fun setUserInfoBean(userInfoBean: UserInfoBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (userInfoBean == null) {
            kv.encode("userInfoBean", "")
            setIsLogin(false)
        } else {
            kv.encode("userInfoBean", Gson().toJson(userInfoBean))
            setIsLogin(true)
        }
    }

    /**
     * 设置账户信息
     */
    fun setUser(userResponse: LoginBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("user", "")
            setIsLogin(false)
        } else {
            kv.encode("user", Gson().toJson(userResponse))
            setIsLogin(true)
        }

    }


    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("login", false)
    }

    /**
     * 设置是否已经登录
     */
    fun setIsLogin(isLogin: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("login", isLogin)
    }

    fun isDarkTheme(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("darkTheme", false)
    }

    fun setDarkTheme(darkTheme: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("darkTheme", darkTheme)
    }

    /**
     * 是否是第一次登陆
     */
    fun isFirst(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("first", true)
    }

    /**
     * 是否是第一次登陆
     */
    fun setFirst(first: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("first", first)
    }

    fun setIsShop(is_shop: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("is_shop", is_shop)
    }

    fun getIsShop(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("is_shop")
    }

    /**
     * 首页是否开启获取指定文章
     */
    fun isNeedTop(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("top", true)
    }

    /**
     * 设置首页是否开启获取指定文章
     */
    fun setIsNeedTop(isNeedTop: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("top", isNeedTop)
    }

    fun getUid(): String {
        return getUser()?.id.toString()
    }


    fun getToken(): String {
        return getUser()?.token.toString()
    }

    fun setLatitude(latitude: String): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("latitude", latitude)
    }

    fun getLatitude(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("latitude") ?: ""
    }

    fun setLongitude(longitude: String): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("longitude", longitude)
    }

    fun getLongitude(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("longitude") ?: ""
    }

    /**
     * 获取搜索历史缓存数据
     */
    fun getSearchHistoryData(): ArrayList<String> {
        val kv = MMKV.mmkvWithID("cache")
        val searchCacheStr = kv.decodeString("history")
        if (!TextUtils.isEmpty(searchCacheStr)) {
            return Gson().fromJson(searchCacheStr, object : TypeToken<ArrayList<String>>() {}.type)
        }
        return arrayListOf()
    }

    fun setSearchHistoryData(searchResponseStr: String) {
        val kv = MMKV.mmkvWithID("cache")
        kv.encode("history", searchResponseStr)
    }

    /**
     * 设置IP
     */
    fun setIP(ip: String) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("ip", ip)
    }

    /**
     * 获取IP
     */
    fun getIP(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("ip") ?: ""
    }


    /**
     * 设置端口号
     */
    fun setHOST(host: String) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("host", host)
    }

    /**
     * 获取口号
     */
    fun getHOST(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("host") ?: ""
    }
}